let index = 1;
import { createMonthArray } from '@/utils/time';

export function createMonthObj(timeRange = []) {
  const ranges = createMonthArray(timeRange);
  return [...ranges, 'total'].reduce((pre, cur) => {
    pre[`pricemonth_${cur}`] = '';
    pre[`fcstmonth_${cur}`] = '';
    pre[`selfpricemonth_${cur}`] = '';
    pre[`selfnumbermonth_${cur}`] = '';
    return pre;
  }, {});
}

export function transferDataDetail(list, timeRange = []) {
  const reg = /^[+]{0,1}(\d+)/;

  return list.map(item => {
    const price = JSON.parse(item.price || item.supplyPlan || '{}');
    const priceFormat = Object.keys(price).reduce((pre, cur) => {
      if (!Number(price[cur]) && Number(price[cur]) !== 0) {
        price[cur] = '';
      }
      pre[`pricemonth_${cur}`] = price[cur] || '';
      return pre;
    }, {});
    const fcst = JSON.parse(
      item.fcst || item.preAllocated || item.salesForecast || '{}'
    );
    const fcstFormat = Object.keys(fcst).reduce((pre, cur) => {
      pre[`fcstmonth_${cur}`] = fcst[cur] || '';
      return pre;
    }, {});

    const lastFcst = JSON.parse(item.lastFcst || '{}');
    const lastFcstFormat = Object.keys(lastFcst).reduce((pre, cur) => {
      pre[`lastFcstmonth_${cur}`] = lastFcst[cur] || '';
      return pre;
    }, {});

    const defaultObj = createMonthObj(timeRange);
    if (!reg.test(item.modelCutRate)) {
      item.modelCutRate = '';
    }
    return {
      ...item,
      id: item.id ? item.id : `random_${++index}`,
      customerCode: {
        key: item.customerCode,
        label: item.customerName,
      },
      modelName: {
        key: item.modelCode,
        label: item.modelName,
      },
      modelNumber: {
        key: item.modelNumber,
        label: item.modelInfoName,
      },

      // modelCutRate: Number(item.modelCutRate) > 0 ? item.modelCutRate : '',
      ...defaultObj,
      ...priceFormat,
      ...fcstFormat,
      ...lastFcstFormat,
      // ...temp,
    };
  });
}
